package com.cimp.controller.user;

import com.cimp.domain.user.User;
import com.cimp.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;


@Controller
public class LoginController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/gotoLogin")
    public String gotoLogin() {
        return "login";
    }

    @RequestMapping("/login")
    protected String login(String username, String password, HttpServletRequest request) {

        if ((!StringUtils.isEmpty(username)) && (!StringUtils.isEmpty(password))) {
            User user = userService.loginUser(username, password);
            if (user != null) {
                System.out.println("登陆成功");
                request.getSession().setAttribute("user",user);
                return "loginSuccessful";
            } else {
                return "login";
            }

        } else {
            System.out.println("登录失败");
            return "login";
        }

    }
}














